package cn.edu.scau.cmi.liangguan.database.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.scau.cmi.liangguan.database.DAO.StudentDAO;
import cn.edu.scau.cmi.liangguan.database.DAO.TutorDAO;
import cn.edu.scau.cmi.liangguan.database.domain.Student;
import cn.edu.scau.cmi.liangguan.database.domain.Tutor;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class DBManageServlet
 */
@WebServlet("/DBManageServlet")
public class DBManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DBManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/json;charset=GBK");
		

		PrintWriter out = response.getWriter();
		String[] str=request.getParameterValues("sendData");
		if (request.getParameter("op").compareTo("1")==0) {
			StudentDAO studentDAO = new StudentDAO();
			Student student = new Student(str[1], str[2]);
			switch(str[0]) {
			case "1":
				if (studentDAO.insertStudent(student)) {
					out.println(JSONArray.fromObject(student));
				}else {
					error(out);
				}
			break;	
			case "2":
				if (studentDAO.updateStudent(student)) {
					out.println(JSONArray.fromObject(student));
				}else {
					error(out);
				}

			break;
			case "3":
				if (studentDAO.deleteStudent(student)) {
					out.println(JSONArray.fromObject(student));
				}else {
					error(out);
				}
			break;	
			default:break;
			}
		}else {
			TutorDAO tutorDAO = new TutorDAO();
			Tutor tutor = new Tutor(str[1], str[2]);
			System.out.println("DBManageServlet.doPost()"+str[1]+" "+str[2]);
			switch(str[0]) {
			case "1":
				if (tutorDAO.insertTutor(tutor)) {
					out.println(JSONArray.fromObject(tutor));
				}else {
					error(out);
				}
			break;	
			case "2":
				if (tutorDAO.updateTutor(tutor)) {
					out.println(JSONArray.fromObject(tutor));
				}else {
					error(out);
				}

			break;
			case "3":
				if (tutorDAO.deleteTutor(tutor)) {
					out.println(JSONArray.fromObject(tutor));
				}else {
					error(out);
				}
			break;	
			default:break;
			}
		}
	}
	protected void error(PrintWriter out) {
		String error = "[{'error':'true'}]";
		JSONArray jsonArray = JSONArray.fromObject(error);
		out.println(jsonArray);			
	}

}
